package com.mdlib.helper.ui.fragments.set;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ljlib.core.util.EmptyUtils;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.model.AccountModel;
import com.mdlib.helper.model.BaseResult;
import com.mdlib.helper.utils.ToastUtil;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 作者：HQY on 16/11/18 16:26
 * 邮箱：hqy_xz@126.com
 */

public class ModifyPasswordFragment extends BaseTitleFragment {


    @Bind(R.id.et_modify_pass_old)
    EditText mEtModifyPassOld;
    @Bind(R.id.et_modify_pass_new)
    EditText mEtModifyPassNew;
    @Bind(R.id.et_modify_pass_new_again)
    EditText mEtModifyPassNewAgain;
    @Bind(R.id.tv_modify_confirm)
    TextView mTvModifyConfirm;

    public static ModifyPasswordFragment newInstance() {
        return new ModifyPasswordFragment();
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_modify_password;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.set_modify_password, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });
    }

    @OnClick(R.id.tv_modify_confirm)
    public void onClick() {
        String oldPass = mEtModifyPassOld.getText().toString();
        String newPass = mEtModifyPassNew.getText().toString();
        String rePass = mEtModifyPassNewAgain.getText().toString();
        if (EmptyUtils.isEmpty(oldPass)) {
            ToastUtil.showToast(getActivity().getResources().getString(R.string.generic_et_not_empty));
            return;
        }
        if (EmptyUtils.isEmpty(newPass)) {
            ToastUtil.showToast(getActivity().getResources().getString(R.string.generic_et_not_empty));
            return;
        }
        if (EmptyUtils.isEmpty(rePass)) {
            ToastUtil.showToast(getActivity().getResources().getString(R.string.generic_et_not_empty));
            return;
        }
        if (!newPass.equals(rePass)) {
            ToastUtil.showToast(getActivity().getResources().getString(R.string.generic_et_passwrod_error));
            return;
        }
        updatePasswrod(oldPass, newPass, rePass);

    }

    private void updatePasswrod(String oldPass, final String newPass, String rePass) {
        startLoadingDialog(true);
        addSubscribe(DataManager.getInstance().getUserApi()
                .modifyPasswrod(oldPass, newPass, rePass)
                .compose(RxHelper.<BaseResult>rxSchedulerHelper())
                .subscribe(new RxSubscribe<BaseResult>() {
                    @Override
                    protected void onSucc(BaseResult baseResult) {
                        stopLoadingDialog();
                        AccountModel.getInstance().setLoginPwd(newPass);
                        AccountModel.getInstance().writeToCache();
                        ToastUtil.showToast("修改密码成功");
                        removeFragment();
                    }

                    @Override
                    protected void onFail(String msg) {
                        stopLoadingDialog();
                        ToastUtil.showToast(msg);

                    }
                }));
    }
}
